﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QDT.Common;
using QDT.Core.Assignments;
using QDT.Core.MSData;
using QDT.Core.Projects;
using QDT.Services.Credits;
using QDT.Services.Deals;
using QDT.Services.Members;
using QDT.Services.Projects;
using QDT.Web.Code;

namespace QDT.Web.Moblie.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/
      
        private readonly IProjectService _projectService;
        private const int ProjectFlag = 870;

        public HomeController(IDealService dealService, IUserService userService, ICreditService creditService, IProjectService projectService)
        {
            _projectService = projectService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ProjectList(int? num)
        {
            num = num == null ? 10 : num;
            int intWait = (int)ProjectStatus.Wait;

            int intBad = (int)ProjectStatus.Bad;

            var filter = PredicateExtensionses.True<Project>();

            filter = filter.And(x => x.TypeID != 7);

            var model = DbProvider.D<Project>()
                                   .Where(
                                       filter.And(
                                           x =>
                                           !x.IsDelete && x.IsEffect && x.InnerStatus != intWait &&
                                           x.InnerStatus != intBad && !x.IsOffline))
                                   .OrderBy(x => x.InnerStatus).ThenByDescending(x => x.ID).ToPagedList(0, num ?? 10);
            ViewBag.pageSize = num + 10;
            return View(model);
        }

        public ActionResult Assignment(int? num)
        {
            num = num == null ? 10 : num;
            var filter = PredicateExtensionses.True<Assignment>();

            var module = DbProvider.D<Assignment>()
                                   .Where(
                                       filter.And(
                                           x => !x.IsDelete && x.IsEffect))
                                   .OrderBy(x => x.InnerStatus).ThenByDescending(x => x.ID).ToPagedList(0, num ?? 10);
            ViewBag.pageSize = num+10;
            return View(module);
        }

      

        public ActionResult ProjectDetail(int id)
        {
            var project = _projectService.GetProject(id);
            if (project == null) return NotFound();
            if (!project.IsEffect || project.IsDelete) return NotFound();
            if (QDTContext.Current.IsAuthenticated && QDTContext.Current.User.GroupID != 1 && project.TypeID == 7&&project.ID>ProjectFlag)
            {
                return NotFound();
            }
         
            return View(project);
        }

        public PartialViewResult Project(int? num=1)
        {
            int intWait = (int)ProjectStatus.Wait;

            int intBad = (int)ProjectStatus.Bad;

            var filter = PredicateExtensionses.True<Project>();

            filter = filter.And(x => x.TypeID != 7);

            var medel = DbProvider.D<Project>()
                                   .Where(
                                       filter.And(
                                           x =>
                                           !x.IsDelete && x.IsEffect && x.InnerStatus != intWait &&
                                           x.InnerStatus != intBad && !x.IsOffline))
                                   .OrderBy(x => x.InnerStatus).ThenByDescending(x => x.ID).ToPagedList(0, num ?? 1);

            return PartialView(medel);
          
        }

        public ActionResult AssignmentDetail()
        {
            return View();
        }

        public ActionResult Temp()
        {
            return View();
        }
    }
}
